#include "lt9211.h"
#include "i2c.h"
#include "mipi_dcs.h"
#include "systick.h"
#include <stdio.h>
#include <stddef.h>

#if 0
//hfp, hs, hbp, hact, htotal, vfp, vs, vbp, vact, vtotal, pixclk
struct video_timing video_640x480_60Hz = {8, 96, 40, 640, 800, 33, 2, 10, 480, 525, 25000};
struct video_timing video_720x480_60Hz = {16, 62, 60, 720, 858, 9, 6, 30, 480, 525, 27000};
//struct video_timing video_800x1280_60Hz = {120, 29, 59, 800, 1008, 2, 2, 8, 1280, 1292, 76000}; //Default HX8394
struct video_timing video_800x1280_60Hz = {20, 20, 5, 800, 845, 20, 20, 20, 1280, 1340, 67938}; // ILI9881

struct video_timing video_1280x720_60Hz = {110, 40, 220, 1280, 1650, 5, 5, 20, 720, 750, 74250};
struct video_timing video_1280x720_30Hz = {110, 40, 220, 1280, 1650, 5, 5, 20, 720, 750, 74250};
struct video_timing video_1366x768_60Hz = {26, 110, 110, 1366, 1592, 13, 6, 13, 768, 800, 81000};
struct video_timing video_1920x720_60Hz = {148, 44, 88, 1920, 2200, 28, 5, 12, 720, 765, 88000};
struct video_timing video_1920x1080_30Hz = {88, 44, 148, 1920, 2200, 4, 5, 36, 1080, 1125, 74250};
struct video_timing video_1920x1080_60Hz = {88, 44, 148, 1920, 2200, 4, 5, 36, 1080, 1125, 148500};
struct video_timing video_1920x1200_60Hz = {48, 32, 80, 1920, 2080, 3, 6, 26, 1200, 1235, 154000};
struct video_timing video_3840x2160_30Hz = {176, 88, 296, 3840, 4400, 8, 10, 72, 2160, 2250, 297000};



void LT9211_VideoCheck(void)
{
	uint8_t sync_polarity;

	HDMI_WriteI2C_Byte(0xff, 0x86);
	HDMI_WriteI2C_Byte(0x20, 0x00);

	sync_polarity = HDMI_ReadI2C_Byte(0x70);
	vs = HDMI_ReadI2C_Byte(0x71);

	hs = HDMI_ReadI2C_Byte(0x72);
	hs = (hs << 8) + HDMI_ReadI2C_Byte(0x73);

	vbp = HDMI_ReadI2C_Byte(0x74);
	vfp = HDMI_ReadI2C_Byte(0x75);

	hbp = HDMI_ReadI2C_Byte(0x76);
	hbp = (hbp << 8) + HDMI_ReadI2C_Byte(0x77);

	hfp = HDMI_ReadI2C_Byte(0x78);
	hfp = (hfp << 8) + HDMI_ReadI2C_Byte(0x79);

	vtotal = HDMI_ReadI2C_Byte(0x7A);
	vtotal = (vtotal << 8) + HDMI_ReadI2C_Byte(0x7B);

	htotal = HDMI_ReadI2C_Byte(0x7C);
	htotal = (htotal << 8) + HDMI_ReadI2C_Byte(0x7D);

	vact = HDMI_ReadI2C_Byte(0x7E);
	vact = (vact << 8) + HDMI_ReadI2C_Byte(0x7F);

	hact = HDMI_ReadI2C_Byte(0x80);
	hact = (hact << 8) + HDMI_ReadI2C_Byte(0x81);

	printf("sync_polarity = %x\r\n", sync_polarity);
	if (!(sync_polarity & 0x01)) //hsync
	{
		HDMI_WriteI2C_Byte(0xff, 0xd8);
		HDMI_WriteI2C_Byte(0x10, (HDMI_ReadI2C_Byte(0x10) | 0x10));
	}
	if (!(sync_polarity & 0x02)) //vsync
	{
		HDMI_WriteI2C_Byte(0xff, 0xd8);
		HDMI_WriteI2C_Byte(0x10, (HDMI_ReadI2C_Byte(0x10) | 0x20));
	}

	printf("hfp, hs, hbp, hact, htotal = %d %d %d %d %d \r\n", hfp, hs, hbp, hact, htotal);
	/*printfdec_uint32_t(hfp);
	printfdec_uint32_t(hs);
	printfdec_uint32_t(hbp);
	printfdec_uint32_t(hact);
	printfdec_uint32_t(htotal);*/

	printf("vfp, vs, vbp, vact, vtotal = %d %d %d %d %d \r\n", vfp, vs, vbp, vact, vtotal);
	/*printfdec_uint32_t(vfp);
	printfdec_uint32_t(vs);
	printfdec_uint32_t(vbp);
	printfdec_uint32_t(vact);
	printfdec_uint32_t(vtotal);*/

	if ((hact == video_800x1280_60Hz.hact) && (vact == video_800x1280_60Hz.vact)) {
		printf("video_800x1280_60Hz\r\n");
		pVideo_Format = &video_800x1280_60Hz;
	} else if ((hact == video_1920x1080_60Hz.hact) && (vact == video_1920x1080_60Hz.vact)) {
		printf("video_1920x1080_60Hz\r\n");
		pVideo_Format = &video_1920x1080_60Hz;
	} else {
		pVideo_Format = NULL;
		printf("video_none\r\n");
	}
}


void LT9211_SetTxTiming(void)
{
	uint16_t hact, vact;
	uint16_t hs, vs;
	uint16_t hbp, vbp;
	//uint16_t htotal, vtotal;
	uint16_t hfp, vfp;

	//__unused(htotal);
	//__unused(vtotal);


	pVideo_Format = &video_800x1280_60Hz;

	hact = pVideo_Format->hact;
	vact = pVideo_Format->vact;
	//htotal = pVideo_Format->htotal;
	//vtotal = pVideo_Format->vtotal;
	// TODO
	hs = pVideo_Format->hs;
	vs = pVideo_Format->vs;
	hfp = pVideo_Format->hfp;
	vfp = pVideo_Format->vfp;
	hbp = pVideo_Format->hbp;
	vbp = pVideo_Format->vbp;

	
	
	//9881
	HDMI_WriteI2C_Byte(0xff, 0xd4);
	HDMI_WriteI2C_Byte(0x04, hs);			//hs[7:0]
	HDMI_WriteI2C_Byte(0x05, hbp);			//hbp[7:0]
	HDMI_WriteI2C_Byte(0x06, hfp);			//hfp[7:0]
	HDMI_WriteI2C_Byte(0x07, (uint8_t)(hact >> 8)); //hactive[15:8]
	HDMI_WriteI2C_Byte(0x08, (uint8_t)(hact));	//hactive[7:0]

	HDMI_WriteI2C_Byte(0x09, (uint8_t)(vs));	//vfp[7:0]
	HDMI_WriteI2C_Byte(0x0a, 0x00);			//bit[3:0]:vbp[11:8]
	HDMI_WriteI2C_Byte(0x0b, (uint8_t)(vbp));	//vbp[7:0]
	HDMI_WriteI2C_Byte(0x0c, (uint8_t)(vact >> 8)); //vcat[15:8]
	HDMI_WriteI2C_Byte(0x0d, (uint8_t)(vact));	//vcat[7:0]
	HDMI_WriteI2C_Byte(0x0e, (uint8_t)(vfp >> 8));	//vfp[11:8]
	HDMI_WriteI2C_Byte(0x0f, (uint8_t)(vfp));	//vfp[7:0]
}


uint8_t LT9211_get_LVDS_Timing(VT *vt)
{
	uint8_t sync_polarity;

	HDMI_WriteI2C_Byte(0xff, 0x86);
	HDMI_WriteI2C_Byte(0x20, 0x00);

	sync_polarity = HDMI_ReadI2C_Byte(0x70);
	
	vt->vs = HDMI_ReadI2C_Byte(0x71);
	vt->hs = HDMI_ReadI2C_Byte(0x72);
	hs = (vt->hs << 8) + HDMI_ReadI2C_Byte(0x73);

	vt->vbp = HDMI_ReadI2C_Byte(0x74);
	vt->vfp = HDMI_ReadI2C_Byte(0x75);

	vt->hbp = HDMI_ReadI2C_Byte(0x76);
	vt->hbp = (vt->hbp << 8) + HDMI_ReadI2C_Byte(0x77);

	vt->hfp = HDMI_ReadI2C_Byte(0x78);
	vt->hfp = (vt->hfp << 8) + HDMI_ReadI2C_Byte(0x79);

	vt->vtotal = HDMI_ReadI2C_Byte(0x7A);
	vt->vtotal = (vtotal << 8) + HDMI_ReadI2C_Byte(0x7B);

	vt->htotal = HDMI_ReadI2C_Byte(0x7C);
	vt->htotal = (vt->htotal << 8) + HDMI_ReadI2C_Byte(0x7D);

	vt->vact = HDMI_ReadI2C_Byte(0x7E);
	vt->vact = (vt->vact << 8) + HDMI_ReadI2C_Byte(0x7F);

	vt->hact = HDMI_ReadI2C_Byte(0x80);
	vt->hact = (vt->hact << 8) + HDMI_ReadI2C_Byte(0x81);

	printf("sync_polarity = %x\r\n", sync_polarity);
	if (!(sync_polarity & 0x01)) //hsync
	{
		HDMI_WriteI2C_Byte(0xff, 0xd8);
		HDMI_WriteI2C_Byte(0x10, (HDMI_ReadI2C_Byte(0x10) | 0x10));
	}
	if (!(sync_polarity & 0x02)) //vsync
	{
		HDMI_WriteI2C_Byte(0xff, 0xd8);
		HDMI_WriteI2C_Byte(0x10, (HDMI_ReadI2C_Byte(0x10) | 0x20));
	}

	printf("hfp, hs, hbp, hact, htotal = %d %d %d %d %d \r\n", hfp, hs, hbp, hact, htotal);
	printf("vfp, vs, vbp, vact, vtotal = %d %d %d %d %d \r\n", vfp, vs, vbp, vact, vtotal);


	/*if ((hact == video_800x1280_60Hz.hact) && (vact == video_800x1280_60Hz.vact)) {
		printf("video_800x1280_60Hz\r\n");
		pVideo_Format = &video_800x1280_60Hz;
	} else if ((hact == video_1920x1080_60Hz.hact) && (vact == video_1920x1080_60Hz.vact)) {
		printf("video_1920x1080_60Hz\r\n");
		pVideo_Format = &video_1920x1080_60Hz;
	} else {
		pVideo_Format = NULL;
		printf("video_none\r\n");
	}*/
	
	return sync_polarity;
}



void old_write_array(void)
{
	DcsPktWrite(dcs0[0], (sizeof(dcs0) - 1), &dcs0[1]);
	DcsPktWrite(dcs1[0], (sizeof(dcs1) - 1), &dcs1[1]);
	DcsPktWrite(dcs2[0], (sizeof(dcs2) - 1), &dcs2[1]);
	DcsPktWrite(dcs3[0], (sizeof(dcs3) - 1), &dcs3[1]);
	DcsPktWrite(dcs4[0], (sizeof(dcs4) - 1), &dcs4[1]);
	DcsPktWrite(dcs5[0], (sizeof(dcs5) - 1), &dcs5[1]);
	DcsPktWrite(dcs6[0], (sizeof(dcs6) - 1), &dcs6[1]);
	DcsPktWrite(dcs7[0], (sizeof(dcs7) - 1), &dcs7[1]);
	DcsPktWrite(dcs8[0], (sizeof(dcs8) - 1), &dcs8[1]);
	DcsPktWrite(dcs9[0], (sizeof(dcs9) - 1), &dcs9[1]);
	DcsPktWrite(dcs10[0], (sizeof(dcs10) - 1), &dcs10[1]);
	DcsPktWrite(dcs11[0], (sizeof(dcs11) - 1), &dcs11[1]);
	DcsPktWrite(dcs12[0], (sizeof(dcs12) - 1), &dcs12[1]);
	DcsPktWrite(dcs13[0], (sizeof(dcs13) - 1), &dcs13[1]);
	DcsPktWrite(dcs14[0], (sizeof(dcs14) - 1), &dcs14[1]);
	DcsPktWrite(dcs15[0], (sizeof(dcs15) - 1), &dcs15[1]);
	DcsPktWrite(dcs16[0], (sizeof(dcs16) - 1), &dcs16[1]);
	DcsPktWrite(dcs17[0], (sizeof(dcs17) - 1), &dcs17[1]);
	DcsPktWrite(dcs18[0], (sizeof(dcs18) - 1), &dcs18[1]);
	DcsPktWrite(dcs19[0], (sizeof(dcs19) - 1), &dcs19[1]);
	DcsPktWrite(dcs20[0], (sizeof(dcs20) - 1), &dcs20[1]);
	DcsPktWrite(dcs21[0], (sizeof(dcs21) - 1), &dcs21[1]);
	DcsPktWrite(dcs22[0], (sizeof(dcs22) - 1), &dcs22[1]);
	DcsPktWrite(dcs23[0], (sizeof(dcs23) - 1), &dcs23[1]);
	DcsPktWrite(dcs24[0], (sizeof(dcs24) - 1), &dcs24[1]);
	DcsPktWrite(dcs25[0], (sizeof(dcs25) - 1), &dcs25[1]);
	DcsPktWrite(dcs26[0], (sizeof(dcs26) - 1), &dcs26[1]);
	DcsPktWrite(dcs27[0], (sizeof(dcs27) - 1), &dcs27[1]);
	DcsPktWrite(dcs28[0], (sizeof(dcs28) - 1), &dcs28[1]);
	DcsPktWrite(dcs29[0], (sizeof(dcs29) - 1), &dcs29[1]);
	DcsPktWrite(dcs30[0], (sizeof(dcs30) - 1), &dcs30[1]);
	DcsPktWrite(dcs31[0], (sizeof(dcs31) - 1), &dcs31[1]);
	DcsPktWrite(dcs32[0], (sizeof(dcs32) - 1), &dcs32[1]);
	DcsPktWrite(dcs33[0], (sizeof(dcs33) - 1), &dcs33[1]);
	DcsPktWrite(dcs34[0], (sizeof(dcs34) - 1), &dcs34[1]);
	DcsPktWrite(dcs35[0], (sizeof(dcs35) - 1), &dcs35[1]);
	DcsPktWrite(dcs36[0], (sizeof(dcs36) - 1), &dcs36[1]);
	DcsPktWrite(dcs37[0], (sizeof(dcs37) - 1), &dcs37[1]);
	DcsPktWrite(dcs38[0], (sizeof(dcs38) - 1), &dcs38[1]);
	DcsPktWrite(dcs39[0], (sizeof(dcs39) - 1), &dcs39[1]);
	DcsPktWrite(dcs40[0], (sizeof(dcs40) - 1), &dcs40[1]);
	DcsPktWrite(dcs41[0], (sizeof(dcs41) - 1), &dcs41[1]);
	DcsPktWrite(dcs42[0], (sizeof(dcs42) - 1), &dcs42[1]);
	DcsPktWrite(dcs43[0], (sizeof(dcs43) - 1), &dcs43[1]);
	DcsPktWrite(dcs44[0], (sizeof(dcs44) - 1), &dcs44[1]);
	DcsPktWrite(dcs45[0], (sizeof(dcs45) - 1), &dcs45[1]);
	DcsPktWrite(dcs46[0], (sizeof(dcs46) - 1), &dcs46[1]);
	DcsPktWrite(dcs47[0], (sizeof(dcs47) - 1), &dcs47[1]);
	DcsPktWrite(dcs48[0], (sizeof(dcs48) - 1), &dcs48[1]);
	DcsPktWrite(dcs49[0], (sizeof(dcs49) - 1), &dcs49[1]);
	DcsPktWrite(dcs50[0], (sizeof(dcs50) - 1), &dcs50[1]);
	DcsPktWrite(dcs51[0], (sizeof(dcs51) - 1), &dcs51[1]);
	DcsPktWrite(dcs52[0], (sizeof(dcs52) - 1), &dcs52[1]);
	DcsPktWrite(dcs53[0], (sizeof(dcs53) - 1), &dcs53[1]);
	DcsPktWrite(dcs54[0], (sizeof(dcs54) - 1), &dcs54[1]);
	DcsPktWrite(dcs55[0], (sizeof(dcs55) - 1), &dcs55[1]);
	DcsPktWrite(dcs56[0], (sizeof(dcs56) - 1), &dcs56[1]);
	DcsPktWrite(dcs57[0], (sizeof(dcs57) - 1), &dcs57[1]);
	DcsPktWrite(dcs58[0], (sizeof(dcs58) - 1), &dcs58[1]);
	DcsPktWrite(dcs59[0], (sizeof(dcs59) - 1), &dcs59[1]);
	DcsPktWrite(dcs60[0], (sizeof(dcs60) - 1), &dcs60[1]);
	DcsPktWrite(dcs61[0], (sizeof(dcs61) - 1), &dcs61[1]);
	DcsPktWrite(dcs62[0], (sizeof(dcs62) - 1), &dcs62[1]);
	DcsPktWrite(dcs63[0], (sizeof(dcs63) - 1), &dcs63[1]);
	DcsPktWrite(dcs64[0], (sizeof(dcs64) - 1), &dcs64[1]);
	DcsPktWrite(dcs65[0], (sizeof(dcs65) - 1), &dcs65[1]);
	DcsPktWrite(dcs66[0], (sizeof(dcs66) - 1), &dcs66[1]);
	DcsPktWrite(dcs67[0], (sizeof(dcs67) - 1), &dcs67[1]);
	DcsPktWrite(dcs68[0], (sizeof(dcs68) - 1), &dcs68[1]);
	DcsPktWrite(dcs69[0], (sizeof(dcs69) - 1), &dcs69[1]);
	DcsPktWrite(dcs70[0], (sizeof(dcs70) - 1), &dcs70[1]);
	DcsPktWrite(dcs71[0], (sizeof(dcs71) - 1), &dcs71[1]);
	DcsPktWrite(dcs72[0], (sizeof(dcs72) - 1), &dcs72[1]);
	DcsPktWrite(dcs73[0], (sizeof(dcs73) - 1), &dcs73[1]);
	DcsPktWrite(dcs74[0], (sizeof(dcs74) - 1), &dcs74[1]);
	DcsPktWrite(dcs75[0], (sizeof(dcs75) - 1), &dcs75[1]);
	DcsPktWrite(dcs76[0], (sizeof(dcs76) - 1), &dcs76[1]);
	DcsPktWrite(dcs77[0], (sizeof(dcs77) - 1), &dcs77[1]);
	DcsPktWrite(dcs78[0], (sizeof(dcs78) - 1), &dcs78[1]);
	DcsPktWrite(dcs79[0], (sizeof(dcs79) - 1), &dcs79[1]);
	DcsPktWrite(dcs80[0], (sizeof(dcs80) - 1), &dcs80[1]);
	DcsPktWrite(dcs81[0], (sizeof(dcs81) - 1), &dcs81[1]);
	DcsPktWrite(dcs82[0], (sizeof(dcs82) - 1), &dcs82[1]);
	DcsPktWrite(dcs83[0], (sizeof(dcs83) - 1), &dcs83[1]);
	DcsPktWrite(dcs84[0], (sizeof(dcs84) - 1), &dcs84[1]);
	DcsPktWrite(dcs85[0], (sizeof(dcs85) - 1), &dcs85[1]);
	DcsPktWrite(dcs86[0], (sizeof(dcs86) - 1), &dcs86[1]);
	DcsPktWrite(dcs87[0], (sizeof(dcs87) - 1), &dcs87[1]);
	DcsPktWrite(dcs88[0], (sizeof(dcs88) - 1), &dcs88[1]);
	DcsPktWrite(dcs89[0], (sizeof(dcs89) - 1), &dcs89[1]);
	DcsPktWrite(dcs90[0], (sizeof(dcs90) - 1), &dcs90[1]);
	DcsPktWrite(dcs91[0], (sizeof(dcs91) - 1), &dcs91[1]);
	DcsPktWrite(dcs92[0], (sizeof(dcs92) - 1), &dcs92[1]);
	DcsPktWrite(dcs93[0], (sizeof(dcs93) - 1), &dcs93[1]);
	DcsPktWrite(dcs94[0], (sizeof(dcs94) - 1), &dcs94[1]);
	DcsPktWrite(dcs95[0], (sizeof(dcs95) - 1), &dcs95[1]);
	DcsPktWrite(dcs96[0], (sizeof(dcs96) - 1), &dcs96[1]);
	DcsPktWrite(dcs97[0], (sizeof(dcs97) - 1), &dcs97[1]);
	DcsPktWrite(dcs98[0], (sizeof(dcs98) - 1), &dcs98[1]);
	DcsPktWrite(dcs99[0], (sizeof(dcs99) - 1), &dcs99[1]);
	DcsPktWrite(dcs100[0], (sizeof(dcs100) - 1), &dcs100[1]);
	DcsPktWrite(dcs101[0], (sizeof(dcs101) - 1), &dcs101[1]);
	DcsPktWrite(dcs102[0], (sizeof(dcs102) - 1), &dcs102[1]);
	DcsPktWrite(dcs103[0], (sizeof(dcs103) - 1), &dcs103[1]);
	DcsPktWrite(dcs104[0], (sizeof(dcs104) - 1), &dcs104[1]);
	DcsPktWrite(dcs105[0], (sizeof(dcs105) - 1), &dcs105[1]);
	DcsPktWrite(dcs106[0], (sizeof(dcs106) - 1), &dcs106[1]);
	DcsPktWrite(dcs107[0], (sizeof(dcs107) - 1), &dcs107[1]);
	DcsPktWrite(dcs108[0], (sizeof(dcs108) - 1), &dcs108[1]);
	DcsPktWrite(dcs109[0], (sizeof(dcs109) - 1), &dcs109[1]);
	DcsPktWrite(dcs110[0], (sizeof(dcs110) - 1), &dcs110[1]);
	DcsPktWrite(dcs111[0], (sizeof(dcs111) - 1), &dcs111[1]);
	DcsPktWrite(dcs112[0], (sizeof(dcs112) - 1), &dcs112[1]);
	DcsPktWrite(dcs113[0], (sizeof(dcs113) - 1), &dcs113[1]);
	DcsPktWrite(dcs114[0], (sizeof(dcs114) - 1), &dcs114[1]);
	DcsPktWrite(dcs115[0], (sizeof(dcs115) - 1), &dcs115[1]);
	DcsPktWrite(dcs116[0], (sizeof(dcs116) - 1), &dcs116[1]);
	DcsPktWrite(dcs117[0], (sizeof(dcs117) - 1), &dcs117[1]);
	DcsPktWrite(dcs118[0], (sizeof(dcs118) - 1), &dcs118[1]);
	DcsPktWrite(dcs119[0], (sizeof(dcs119) - 1), &dcs119[1]);
	DcsPktWrite(dcs120[0], (sizeof(dcs120) - 1), &dcs120[1]);
	DcsPktWrite(dcs121[0], (sizeof(dcs121) - 1), &dcs121[1]);
	DcsPktWrite(dcs122[0], (sizeof(dcs122) - 1), &dcs122[1]);
	DcsPktWrite(dcs123[0], (sizeof(dcs123) - 1), &dcs123[1]);
	DcsPktWrite(dcs124[0], (sizeof(dcs124) - 1), &dcs124[1]);
	DcsPktWrite(dcs125[0], (sizeof(dcs125) - 1), &dcs125[1]);
	DcsPktWrite(dcs126[0], (sizeof(dcs126) - 1), &dcs126[1]);
	DcsPktWrite(dcs127[0], (sizeof(dcs127) - 1), &dcs127[1]);
	DcsPktWrite(dcs128[0], (sizeof(dcs128) - 1), &dcs128[1]);
	DcsPktWrite(dcs129[0], (sizeof(dcs129) - 1), &dcs129[1]);
	DcsPktWrite(dcs130[0], (sizeof(dcs130) - 1), &dcs130[1]);
	DcsPktWrite(dcs131[0], (sizeof(dcs131) - 1), &dcs131[1]);
	DcsPktWrite(dcs132[0], (sizeof(dcs132) - 1), &dcs132[1]);
	DcsPktWrite(dcs133[0], (sizeof(dcs133) - 1), &dcs133[1]);
	DcsPktWrite(dcs134[0], (sizeof(dcs134) - 1), &dcs134[1]);
	DcsPktWrite(dcs135[0], (sizeof(dcs135) - 1), &dcs135[1]);
	DcsPktWrite(dcs136[0], (sizeof(dcs136) - 1), &dcs136[1]);
	DcsPktWrite(dcs137[0], (sizeof(dcs137) - 1), &dcs137[1]);
	DcsPktWrite(dcs138[0], (sizeof(dcs138) - 1), &dcs138[1]);
	DcsPktWrite(dcs139[0], (sizeof(dcs139) - 1), &dcs139[1]);
	DcsPktWrite(dcs140[0], (sizeof(dcs140) - 1), &dcs140[1]);
	DcsPktWrite(dcs141[0], (sizeof(dcs141) - 1), &dcs141[1]); // ff 98 81 01
	DcsPktWrite(dcs142[0], (sizeof(dcs142) - 1), &dcs142[1]); // 22 0a
	DcsPktWrite(dcs143[0], (sizeof(dcs143) - 1), &dcs143[1]); // 25 vfp
	DcsPktWrite(dcs144[0], (sizeof(dcs144) - 1), &dcs144[1]); // 26 vbp
	DcsPktWrite(dcs145[0], (sizeof(dcs145) - 1), &dcs145[1]); // 27 hbp & 0xff
	DcsPktWrite(dcs146[0], (sizeof(dcs146) - 1), &dcs146[1]); // 27 hbp >> 8
	DcsPktWrite(dcs147[0], (sizeof(dcs147) - 1), &dcs147[1]);
	DcsPktWrite(dcs148[0], (sizeof(dcs148) - 1), &dcs148[1]);
	DcsPktWrite(dcs149[0], (sizeof(dcs149) - 1), &dcs149[1]);
	DcsPktWrite(dcs150[0], (sizeof(dcs150) - 1), &dcs150[1]);
	DcsPktWrite(dcs151[0], (sizeof(dcs151) - 1), &dcs151[1]);
	DcsPktWrite(dcs152[0], (sizeof(dcs152) - 1), &dcs152[1]);
	DcsPktWrite(dcs153[0], (sizeof(dcs153) - 1), &dcs153[1]);
	DcsPktWrite(dcs154[0], (sizeof(dcs154) - 1), &dcs154[1]);
	DcsPktWrite(dcs155[0], (sizeof(dcs155) - 1), &dcs155[1]);
	DcsPktWrite(dcs156[0], (sizeof(dcs156) - 1), &dcs156[1]);
	DcsPktWrite(dcs157[0], (sizeof(dcs157) - 1), &dcs157[1]);
	DcsPktWrite(dcs158[0], (sizeof(dcs158) - 1), &dcs158[1]);
	DcsPktWrite(dcs159[0], (sizeof(dcs159) - 1), &dcs159[1]);
	DcsPktWrite(dcs160[0], (sizeof(dcs160) - 1), &dcs160[1]);
	DcsPktWrite(dcs161[0], (sizeof(dcs161) - 1), &dcs161[1]);
	DcsPktWrite(dcs162[0], (sizeof(dcs162) - 1), &dcs162[1]);
	DcsPktWrite(dcs163[0], (sizeof(dcs163) - 1), &dcs163[1]);
	DcsPktWrite(dcs164[0], (sizeof(dcs164) - 1), &dcs164[1]);
	DcsPktWrite(dcs165[0], (sizeof(dcs165) - 1), &dcs165[1]);
	DcsPktWrite(dcs166[0], (sizeof(dcs166) - 1), &dcs166[1]);
	DcsPktWrite(dcs167[0], (sizeof(dcs167) - 1), &dcs167[1]);
	DcsPktWrite(dcs168[0], (sizeof(dcs168) - 1), &dcs168[1]);
	DcsPktWrite(dcs169[0], (sizeof(dcs169) - 1), &dcs169[1]);
	DcsPktWrite(dcs170[0], (sizeof(dcs170) - 1), &dcs170[1]);
	DcsPktWrite(dcs171[0], (sizeof(dcs171) - 1), &dcs171[1]);
	DcsPktWrite(dcs172[0], (sizeof(dcs172) - 1), &dcs172[1]);
	DcsPktWrite(dcs173[0], (sizeof(dcs173) - 1), &dcs173[1]);
	DcsPktWrite(dcs174[0], (sizeof(dcs174) - 1), &dcs174[1]);
	DcsPktWrite(dcs175[0], (sizeof(dcs175) - 1), &dcs175[1]);
	DcsPktWrite(dcs176[0], (sizeof(dcs176) - 1), &dcs176[1]);
	DcsPktWrite(dcs177[0], (sizeof(dcs177) - 1), &dcs177[1]);
	DcsPktWrite(dcs178[0], (sizeof(dcs178) - 1), &dcs178[1]);
	DcsPktWrite(dcs179[0], (sizeof(dcs179) - 1), &dcs179[1]);
	DcsPktWrite(dcs180[0], (sizeof(dcs180) - 1), &dcs180[1]);
	DcsPktWrite(dcs181[0], (sizeof(dcs181) - 1), &dcs181[1]);
	DcsPktWrite(dcs182[0], (sizeof(dcs182) - 1), &dcs182[1]);
	DcsPktWrite(dcs183[0], (sizeof(dcs183) - 1), &dcs183[1]);
	DcsPktWrite(dcs184[0], (sizeof(dcs184) - 1), &dcs184[1]);
	DcsPktWrite(dcs185[0], (sizeof(dcs185) - 1), &dcs185[1]);
	DcsPktWrite(dcs186[0], (sizeof(dcs186) - 1), &dcs186[1]);
	DcsPktWrite(dcs187[0], (sizeof(dcs187) - 1), &dcs187[1]);
	DcsPktWrite(dcs188[0], (sizeof(dcs188) - 1), &dcs188[1]);
	DcsPktWrite(dcs189[0], (sizeof(dcs189) - 1), &dcs189[1]);
	DcsPktWrite(dcs190[0], (sizeof(dcs190) - 1), &dcs190[1]);
	DcsPktWrite(dcs191[0], (sizeof(dcs191) - 1), &dcs191[1]);
	delay_1ms(200);
	DcsPktWrite(dcs192[0], (sizeof(dcs192) - 1), &dcs192[1]);
	delay_1ms(10);
	DcsPktWrite(dcs193[0], (sizeof(dcs193) - 1), &dcs193[1]);
}


uint8_t dcs0[]={0x29,0xFF,0x98,0x81,0x03};
uint8_t dcs1[]={0x23,0x01,0x00};
uint8_t dcs2[]={0x23,0x02,0x00};
uint8_t dcs3[]={0x23,0x03,0x54};
uint8_t dcs4[]={0x23,0x04,0xD4};
uint8_t dcs5[]={0x23,0x05,0x00};
uint8_t dcs6[]={0x23,0x06,0x11};
uint8_t dcs7[]={0x23,0x07,0x09};
uint8_t dcs8[]={0x23,0x08,0x00};
uint8_t dcs9[]={0x23,0x09,0x00};
uint8_t dcs10[]={0x23,0x0a,0x00};
uint8_t dcs11[]={0x23,0x0b,0x00};
uint8_t dcs12[]={0x23,0x0c,0x00};
uint8_t dcs13[]={0x23,0x0d,0x00};
uint8_t dcs14[]={0x23,0x0e,0x00};
uint8_t dcs15[]={0x23,0x0f,0x26};
uint8_t dcs16[]={0x23,0x10,0x26};
uint8_t dcs17[]={0x23,0x11,0x00};
uint8_t dcs18[]={0x23,0x12,0x00};
uint8_t dcs19[]={0x23,0x13,0x00};
uint8_t dcs20[]={0x23,0x14,0x00};
uint8_t dcs21[]={0x23,0x15,0x00};
uint8_t dcs22[]={0x23,0x16,0x00};
uint8_t dcs23[]={0x23,0x17,0x00};
uint8_t dcs24[]={0x23,0x18,0x00};
uint8_t dcs25[]={0x23,0x19,0x00};
uint8_t dcs26[]={0x23,0x1a,0x00};
uint8_t dcs27[]={0x23,0x1b,0x00};
uint8_t dcs28[]={0x23,0x1c,0x00};
uint8_t dcs29[]={0x23,0x1d,0x00};
uint8_t dcs30[]={0x23,0x1e,0x40};
uint8_t dcs31[]={0x23,0x1f,0x80};
uint8_t dcs32[]={0x23,0x20,0x06};
uint8_t dcs33[]={0x23,0x21,0x01};
uint8_t dcs34[]={0x23,0x22,0x00};
uint8_t dcs35[]={0x23,0x23,0x00};
uint8_t dcs36[]={0x23,0x24,0x00};
uint8_t dcs37[]={0x23,0x25,0x00};
uint8_t dcs38[]={0x23,0x26,0x00};
uint8_t dcs39[]={0x23,0x27,0x00};
uint8_t dcs40[]={0x23,0x28,0x33};
uint8_t dcs41[]={0x23,0x29,0x33};
uint8_t dcs42[]={0x23,0x2a,0x00};
uint8_t dcs43[]={0x23,0x2b,0x00};
uint8_t dcs44[]={0x23,0x2c,0x00};
uint8_t dcs45[]={0x23,0x2d,0x00};
uint8_t dcs46[]={0x23,0x2e,0x00};
uint8_t dcs47[]={0x23,0x2f,0x00};
uint8_t dcs48[]={0x23,0x30,0x00};
uint8_t dcs49[]={0x23,0x31,0x00};
uint8_t dcs50[]={0x23,0x32,0x00};
uint8_t dcs51[]={0x23,0x33,0x00};
uint8_t dcs52[]={0x23,0x34,0x03};
uint8_t dcs53[]={0x23,0x35,0x00};
uint8_t dcs54[]={0x23,0x36,0x00};
uint8_t dcs55[]={0x23,0x37,0x00};
uint8_t dcs56[]={0x23,0x38,0x96};
uint8_t dcs57[]={0x23,0x39,0x00};
uint8_t dcs58[]={0x23,0x3a,0x00};
uint8_t dcs59[]={0x23,0x3b,0x00};
uint8_t dcs60[]={0x23,0x3c,0x00};
uint8_t dcs61[]={0x23,0x3d,0x00};
uint8_t dcs62[]={0x23,0x3e,0x00};
uint8_t dcs63[]={0x23,0x3f,0x00};
uint8_t dcs64[]={0x23,0x40,0x00};
uint8_t dcs65[]={0x23,0x41,0x00};
uint8_t dcs66[]={0x23,0x42,0x00};
uint8_t dcs67[]={0x23,0x43,0x00};
uint8_t dcs68[]={0x23,0x44,0x00};
uint8_t dcs69[]={0x23,0x50,0x00};
uint8_t dcs70[]={0x23,0x51,0x23};
uint8_t dcs71[]={0x23,0x52,0x45};
uint8_t dcs72[]={0x23,0x53,0x67};
uint8_t dcs73[]={0x23,0x54,0x89};
uint8_t dcs74[]={0x23,0x55,0xab};
uint8_t dcs75[]={0x23,0x56,0x01};
uint8_t dcs76[]={0x23,0x57,0x23};
uint8_t dcs77[]={0x23,0x58,0x45};
uint8_t dcs78[]={0x23,0x59,0x67};
uint8_t dcs79[]={0x23,0x5a,0x89};
uint8_t dcs80[]={0x23,0x5b,0xab};
uint8_t dcs81[]={0x23,0x5c,0xcd};
uint8_t dcs82[]={0x23,0x5d,0xef};
uint8_t dcs83[]={0x23,0x5e,0x00};
uint8_t dcs84[]={0x23,0x5f,0x0D};
uint8_t dcs85[]={0x23,0x60,0x0D};
uint8_t dcs86[]={0x23,0x61,0x0C};
uint8_t dcs87[]={0x23,0x62,0x0C};
uint8_t dcs88[]={0x23,0x63,0x0F};
uint8_t dcs89[]={0x23,0x64,0x0F};
uint8_t dcs90[]={0x23,0x65,0x0E};
uint8_t dcs91[]={0x23,0x66,0x0E};
uint8_t dcs92[]={0x23,0x67,0x08};
uint8_t dcs93[]={0x23,0x68,0x02};
uint8_t dcs94[]={0x23,0x69,0x02};
uint8_t dcs95[]={0x23,0x6a,0x02};
uint8_t dcs96[]={0x23,0x6b,0x02};
uint8_t dcs97[]={0x23,0x6c,0x02};
uint8_t dcs98[]={0x23,0x6d,0x02};
uint8_t dcs99[]={0x23,0x6e,0x02};
uint8_t dcs100[]={0x23,0x6f,0x02};
uint8_t dcs101[]={0x23,0x70,0x14};
uint8_t dcs102[]={0x23,0x71,0x15};
uint8_t dcs103[]={0x23,0x72,0x06};
uint8_t dcs104[]={0x23,0x73,0x02};
uint8_t dcs105[]={0x23,0x74,0x02};
uint8_t dcs106[]={0x23,0x75,0x0D};
uint8_t dcs107[]={0x23,0x76,0x0D};
uint8_t dcs108[]={0x23,0x77,0x0C};
uint8_t dcs109[]={0x23,0x78,0x0C};
uint8_t dcs110[]={0x23,0x79,0x0F};
uint8_t dcs111[]={0x23,0x7a,0x0F};
uint8_t dcs112[]={0x23,0x7b,0x0E};
uint8_t dcs113[]={0x23,0x7c,0x0E};
uint8_t dcs114[]={0x23,0x7d,0x08};
uint8_t dcs115[]={0x23,0x7e,0x02};
uint8_t dcs116[]={0x23,0x7f,0x02};
uint8_t dcs117[]={0x23,0x80,0x02};
uint8_t dcs118[]={0x23,0x81,0x02};
uint8_t dcs119[]={0x23,0x82,0x02};
uint8_t dcs120[]={0x23,0x83,0x02};
uint8_t dcs121[]={0x23,0x84,0x02};
uint8_t dcs122[]={0x23,0x85,0x02};
uint8_t dcs123[]={0x23,0x86,0x14};
uint8_t dcs124[]={0x23,0x87,0x15};
uint8_t dcs125[]={0x23,0x88,0x06};
uint8_t dcs126[]={0x23,0x89,0x02};
uint8_t dcs127[]={0x23,0x8A,0x02};
uint8_t dcs128[]={0x29,0xFF,0x98,0x81,0x04};
uint8_t dcs129[]={0x23,0x6E,0x3B};
uint8_t dcs130[]={0x23,0x6F,0x57};
uint8_t dcs131[]={0x23,0x3A,0x24};
uint8_t dcs132[]={0x23,0x8D,0x1F};
uint8_t dcs133[]={0x23,0x87,0xBA};
uint8_t dcs134[]={0x23,0xB2,0xD1};
uint8_t dcs135[]={0x23,0x88,0x0B};
uint8_t dcs136[]={0x23,0x38,0x01};
uint8_t dcs137[]={0x23,0x39,0x00};
uint8_t dcs138[]={0x23,0xB5,0x07};
uint8_t dcs139[]={0x23,0x31,0x75};
uint8_t dcs140[]={0x23,0x3B,0x98};
uint8_t dcs141[]={0x29,0xFF,0x98,0x81,0x01};
uint8_t dcs142[]={0x23,0x22,0x0A};
uint8_t dcs143[]={0x23,0x31,0x09};
uint8_t dcs144[]={0x23,0x53,0x7B};
uint8_t dcs145[]={0x23,0x55,0x40};
uint8_t dcs146[]={0x23,0x50,0x86};
uint8_t dcs147[]={0x23,0x51,0x82};
uint8_t dcs148[]={0x23,0x60,0x27};
uint8_t dcs149[]={0x23,0x62,0x20};
uint8_t dcs150[]={0x23,0xA0,0x00};
uint8_t dcs151[]={0x23,0xA1,0x12};
uint8_t dcs152[]={0x23,0xA2,0x20};
uint8_t dcs153[]={0x23,0xA3,0x13};
uint8_t dcs154[]={0x23,0xA4,0x14};
uint8_t dcs155[]={0x23,0xA5,0x27};
uint8_t dcs156[]={0x23,0xA6,0x1D};
uint8_t dcs157[]={0x23,0xA7,0x1F};
uint8_t dcs158[]={0x23,0xA8,0x7C};
uint8_t dcs159[]={0x23,0xA9,0x1D};
uint8_t dcs160[]={0x23,0xAA,0x2A};
uint8_t dcs161[]={0x23,0xAB,0x6B};
uint8_t dcs162[]={0x23,0xAC,0x1A};
uint8_t dcs163[]={0x23,0xAD,0x18};
uint8_t dcs164[]={0x23,0xAE,0x4E};
uint8_t dcs165[]={0x23,0xAF,0x24};
uint8_t dcs166[]={0x23,0xB0,0x2A};
uint8_t dcs167[]={0x23,0xB1,0x4D};
uint8_t dcs168[]={0x23,0xB2,0x5B};
uint8_t dcs169[]={0x23,0xB3,0x23};
uint8_t dcs170[]={0x23,0xC0,0x00};
uint8_t dcs171[]={0x23,0xC1,0x13};
uint8_t dcs172[]={0x23,0xC2,0x20};
uint8_t dcs173[]={0x23,0xC3,0x12};
uint8_t dcs174[]={0x23,0xC4,0x15};
uint8_t dcs175[]={0x23,0xC5,0x28};
uint8_t dcs176[]={0x23,0xC6,0x1C};
uint8_t dcs177[]={0x23,0xC7,0x1E};
uint8_t dcs178[]={0x23,0xC8,0x7B};
uint8_t dcs179[]={0x23,0xC9,0x1E};
uint8_t dcs180[]={0x23,0xCA,0x29};
uint8_t dcs181[]={0x23,0xCB,0x6C};
uint8_t dcs182[]={0x23,0xCC,0x1A};
uint8_t dcs183[]={0x23,0xCD,0x19};
uint8_t dcs184[]={0x23,0xCE,0x4D};
uint8_t dcs185[]={0x23,0xCF,0x22};
uint8_t dcs186[]={0x23,0xD0,0x2A};
uint8_t dcs187[]={0x23,0xD1,0x4D};
uint8_t dcs188[]={0x23,0xD2,0x5B};
uint8_t dcs189[]={0x23,0xD3,0x23};
uint8_t dcs190[]={0x29,0xFF,0x98,0x81,0x00};
uint8_t dcs191[]={0x23,0x11,0x00};
uint8_t dcs192[]={0x23,0x29,0x00};
uint8_t dcs193[]={0x23,0x35,0x00};

// mipi_dcs.h
void DcsPktWrite(uint8_t DCS_DI, uint8_t Len, uint8_t *Ptr);

// mipi_dcs.c
void DcsPktWrite(uint8_t DCS_DI, uint8_t Len, uint8_t *Ptr)
{
	uint8_t i = 0;
	//printf("reg %02x, pkg len %02x\r\n", *Ptr, Len);
	HDMI_WriteI2C_Byte(0xff, 0xd4);
	if (Len == 2) {
		HDMI_WriteI2C_Byte(0x01, 0x0c);
		HDMI_WriteI2C_Byte(0x02, 0x04);
		HDMI_WriteI2C_Byte(0x03, DCS_DI);
		HDMI_WriteI2C_Byte(0x03, *Ptr);
		HDMI_WriteI2C_Byte(0x03, *(Ptr + 1));
	} else {
		HDMI_WriteI2C_Byte(0x01, 0x0E);
		HDMI_WriteI2C_Byte(0x02, Len + 6);
		HDMI_WriteI2C_Byte(0x03, DCS_DI);
		HDMI_WriteI2C_Byte(0x03, Len);
		HDMI_WriteI2C_Byte(0x03, 0x00);
	
		for (i = 0; i < Len; i++) {
			HDMI_WriteI2C_Byte(0x03, *Ptr);
			Ptr++;
		}
	}
	delay_1ms(1);
	HDMI_WriteI2C_Byte(0x01, 0x00);
}
// mipi_dcs.h
void Generic_Short_Write_1P(uint8_t data0, uint8_t data1);

// mipi_dcs.c
uint8_t dcsrst[] = {0x05, 0x01, 0x00};
//*************************************
// Sleep-Out
//*************************************
uint8_t Sleep_Out[] = {0x05, 0x11, 0x00};
uint8_t Sleep_IN[] = {0x05, 0x10, 0x00};
//DELAY	120
// Display-On
//*************************************
uint8_t Display_On[] = {0x05, 0x29, 0x00};

#define CMDNUM (10)
void Generic_Short_Write_1P(uint8_t data0, uint8_t data1)
{
	HDMI_WriteI2C_Byte(0xff, 0xd4);
	HDMI_WriteI2C_Byte(0x01, 0x0c);
	HDMI_WriteI2C_Byte(0x02, 0x04);
	HDMI_WriteI2C_Byte(0x03, 0x15);
	HDMI_WriteI2C_Byte(0x03, data0);
	HDMI_WriteI2C_Byte(0x03, data1);
	delay_1ms(1);
	HDMI_WriteI2C_Byte(0x01, 0x00);
}


// lt9211.c
int lt9211_lvds_clkstb_check(void)
{
	uint8_t porta_clk_state = 0;
	uint8_t portb_clk_state = 0;

	HDMI_WriteI2C_Byte(0xff, 0x86);
	HDMI_WriteI2C_Byte(0x00, 0x01);
	delay_1ms(300);
	porta_clk_state = (HDMI_ReadI2C_Byte(0x08) & (0x20));

	HDMI_WriteI2C_Byte(0xff, 0x86);
	HDMI_WriteI2C_Byte(0x00, 0x02);
	delay_1ms(300);
	portb_clk_state = (HDMI_ReadI2C_Byte(0x08) & (0x20));

	if (INPUT_PORT_NUM == 1) {
#ifdef INPUT_PORTA
		if (porta_clk_state) {
			return 1;
		} else {
			return 0;
		}
#endif
#ifdef INPUT_PORTB
		if (portb_clk_state) {
			return 1;
		} else {
			return 0;
		}
#endif
	} else if (INPUT_PORT_NUM == 2) {
		if (porta_clk_state && portb_clk_state) {
			return 1;
		} else {
			return 0;
		}
	}
}

// lt9211.c
void LT9211_ClockCheckDebug(void)
{
#ifdef _uart_debug_
	uint32_t fm_value;

	lvds_clk_in = 0;
#ifdef INPUT_PORTA
	HDMI_WriteI2C_Byte(0xff, 0x86);
	HDMI_WriteI2C_Byte(0x00, 0x01);
	delay_1ms(50);
	fm_value = 0;
	fm_value = (HDMI_ReadI2C_Byte(0x08) & (0x0f));
	fm_value = (fm_value << 8);
	fm_value = fm_value + HDMI_ReadI2C_Byte(0x09);
	fm_value = (fm_value << 8);
	fm_value = fm_value + HDMI_ReadI2C_Byte(0x0a);
	printf("Port A lvds clock: \r\n");
	printfdec_uint32_t(fm_value);
	lvds_clk_in = fm_value;
#endif

#ifdef INPUT_PORTB
	HDMI_WriteI2C_Byte(0xff, 0x86);
	HDMI_WriteI2C_Byte(0x00, 0x02);
	delay_1ms(50);
	fm_value = 0;
	fm_value = (HDMI_ReadI2C_Byte(0x08) & (0x0f));
	fm_value = (fm_value << 8);
	fm_value = fm_value + HDMI_ReadI2C_Byte(0x09);
	fm_value = (fm_value << 8);
	fm_value = fm_value + HDMI_ReadI2C_Byte(0x0a);
	printf("Port B lvds clock: \r\n");
	printfdec_uint32_t(fm_value);
	lvds_clk_in = fm_value;
#endif

#endif
}

int LT9211_Check_LVDS_Clock(void)
{
	uint8_t porta_clk_state = 0;

	HDMI_WriteI2C_Byte(0xff, 0x86);
	HDMI_WriteI2C_Byte(0x00, 0x01);

	/*uint8_t reg08 = HDMI_ReadI2C_Byte(0x08);
	uint8_t reg09 = HDMI_ReadI2C_Byte(0x09);
	uint8_t reg0a = HDMI_ReadI2C_Byte(0x0A);*/
	// TODO

	porta_clk_state = (HDMI_ReadI2C_Byte(0x08) & (0x20));

	return porta_clk_state;
}
#endif
